package com.medata.utils;

import com.medata.dataObject.Docobject;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;

import java.io.File;
import java.io.IOException;

/**
 * @author ：zb
 * @date ：Created in 2019/5/24 0024 17:12
 * @description：
 */

public class PdfUtil {

    public static Docobject getPdfObject(String dbaname,Docobject docobject, String filePath) throws IOException {
        String path=filePath+"/"+dbaname+"/"+docobject.getServerfullpath();
        PDDocument document=PDDocument.load(new File(path));
        PDPageTree pdft=document.getDocumentCatalog().getPages();
        PDDocument doc=new PDDocument();
        PDPage page1 = pdft.get(0);
        PDPage page2 = pdft.get(document.getNumberOfPages() - 1);
        doc.addPage(page1);
        doc.addPage(page2);
        String fileName="压缩版_"+docobject.getObjectfilename();
//        String newPath=filePath+"压缩版_"+UidUtil.getMid()+"_"+docobject.getObjectfilename();
        String serverpath="/"+docobject.getDocid()+"/"+"压缩版_"+docobject.getObjectfilename();
        String newPath=filePath+"/"+dbaname+"/"+docobject.getDocid()+"/"+"压缩版_"+docobject.getObjectfilename();
        doc.save(new File(newPath));
        doc.close();
        document.close();
        docobject.setObjectfilename(fileName);
        docobject.setServerfullpath(serverpath);
        return docobject;
    }

//    public static void main(String[] args)throws Exception {
//        PDDocument document=PDDocument.load(new File("c:/test/2019052485-2019x的合同.pdf"));
//        PDPageTree pdft=document.getDocumentCatalog().getPages();
//        PDDocument doc=new PDDocument();
//        PDPage page1 = pdft.get(0);
//        PDPage page2 = pdft.get(document.getNumberOfPages() - 1);
//        doc.addPage(page1);
//        doc.addPage(page2);
//        doc.save(new File("c:/test/bb.pdf"));
//        doc.close();
//    }

}
